﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Collections;
using System.Xml;
using System.Data;

namespace ZemantaConnector
{
    public class ZemantaConnector 
    {
        private string ApiKey = "ar4th2pmgggq3rnzqwsb4w5e";

        public string getZemanta(string whichMethod, string whatContent, string whatFormat)
        {
            Uri address = new Uri("http://api.zemanta.com/services/rest/0.0/");

            // we need to create the web request
            HttpWebRequest wreq = WebRequest.Create(address) as HttpWebRequest;

            // we want to post the data - so set that here.
            wreq.Method = "POST";
            wreq.ContentType = "application/x-www-form-urlencoded";

            // build string with data for REST
            StringBuilder d = new StringBuilder();
            d.Append("method=" + System.Security.SecurityElement.Escape(whichMethod));
            d.Append("&api_key=" + System.Security.SecurityElement.Escape(ApiKey));
            d.Append("&text=" + System.Security.SecurityElement.Escape(whatContent));
            d.Append("&format=" + System.Security.SecurityElement.Escape(whatFormat));

            // break it down to a byte array
            byte[] bd = UTF8Encoding.UTF8.GetBytes(d.ToString());

            // set length & write
            wreq.ContentLength = bd.Length;
            using (Stream ps = wreq.GetRequestStream()) { ps.Write(bd, 0, bd.Length); }

            // response?
            using (HttpWebResponse wres = wreq.GetResponse() as HttpWebResponse)
            {
                // capture the response
                StreamReader r = new StreamReader(wres.GetResponseStream());

                // return the results
                return r.ReadToEnd();
            }
        }
    }
}

